package com.example.demo.concurrent.Lock.AQS;

import lombok.extern.slf4j.Slf4j;

//不可重入锁
@Slf4j(topic = "c.TestAQS")
public class TestAQS {
    public static void main2(String[] args) {
        MyLock lock = new MyLock();
        new Thread(()->{
            lock.lock();
            try{
                log.debug("locking...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }finally {
                log.debug("unlocking...");
                lock.unlock();
            }
        },"t1").start();
        new Thread(()->{
            lock.lock();
            try{
                log.debug("locking...");
            }finally {
                log.debug("unlocking...");
                lock.unlock();
            }
        },"t2").start();
    }

    /**
     * 不可重入锁
     * 本线程加了锁之后,下次同一个线程来还要加锁,加不上了-->不能重复加锁,必须等待锁释放之后才能继续加锁
     * @param args
     */
    public static void main(String[] args) {
        MyLock lock = new MyLock();
        new Thread(()->{
            lock.lock();
            log.debug("locking...");
            lock.lock();
            log.debug("locking...");
        },"t1").start();
    }
}